<?php
	namespace Util;
	/**
	 * [ForCate 无限分级]
	 * @Author   :seven
	 * @DateTime :2016-07-05T14:18:22+0800
	 * @Detail   : 
	 * 
	 */
	class ForCate{
		/**
		 * [unlimited 实现无限分级]
		 * @Author   :seven
		 * @DateTime :2016-07-05T14:18:22+0800
		 * @Detail   :
		 * @param    [Array]	$cate    [数据包]
		 * @param    string		$name    [子分组名称]
		 * @param    string 	$pid     [PID名称]
		 * @param    string   	$mid     [MYSELF名称]
		 * @param    [type]  	$pidSign [PID代码]
		 * @param    [type]  	$sumSign [包含子分组]
		 * @return   [type]  			 [结果集]
		 */
		Static public function unlimited($cate,$name='FORWARD',$pid='PID',$mid='MID',$pidSign,$sumSign){
			$result = array();

			foreach ($cate as $k => $v) {	
				if($v[$pid] == $pidSign){
					unset($v[$k]);
					$v[$name] = self::unlimited($cate,$name,$pid,$mid,$v[$mid],$sumSign);
					
					if(!empty($sumSign) && isset($v[$name])){
						$v[$sumSign] = count($v[$name]);
						foreach ($v[$name] as $value){
							$v[$sumSign] += $value[$sumSign];
						}
					}
					
					$result[]=$v;
				}
			}

			return $result;
		}

		/**
		 * [getParents 获取所有结点]
		 * @Author   :seven
		 * @DateTime :2016-05-10T22:58:56+0800
		 * @Detail   :
		 * @param    [Array]	$cate      [Data Array]
		 * @param    [String]	$id        [MID参数]
		 * @param    string 	$pidname   [PID名称]
		 * @param    string 	$idname    [ID名称]
		 * @return   [Array]    [Target Array]
		 */
		Static public function getParents($data,$id,$pidname='PID',$idname='MID'){
			$res = array();
			foreach($data as $v){
				if($v[$idname] == $id){
					$res[] = $v;
					$res = array_merge(self::getParents($data,$v[$pidname]),$res,$pidname,$idname);
				}
			}
			return $res;
		}

		/**
		 * [getChilds 获取当前PID的下一级子集]
		 * @Author   :seven
		 * @DateTime :2016-05-10T23:29:04+0800
		 * @Detail   :
		 * @param    [Array]	$cate    [Date Array]
		 * @param    [type]		$pid     [PID]
		 * @param    string 	$pidname [PID]
		 * @param    string 	$idname  [MID]
		 * @return   [Array]		     [结果集]
		 */
		Static public function getChilds($cate,$pid,$pidname='PID',$idname='MID'){
			$res = array();
			foreach ($cate as $v) {
				if ($v[$pidname] == $pid) {
					$res[]=$v;
					$res = array_merge($res,self::getChilds($cate,$v[$idname],$pidname,$idname));
				}
			}
			return $res;
		}


		/**
		 * [setAddressCache 地址数据库转化JSON包]
		 * @Author   :seven
		 * @DateTime :2016-07-07T09:11:46+0800
		 * @Detail   :
		 * @param    [type]		$data [description]
		 */
		public function setAddressCache($data){
			$data = \Util\ForCate::unlimited($data,'subList','pid','id','1');
			$this->BuildCache($data,'AREA_ADDRESS_PROVINCE',0);
		}

		/**
		 * [BuildCache 创建缓存文件]
		 * @Author   :seven
		 * @DateTime :2016-07-07T09:13:53+0800
		 * @Detail   :
		 * @param    [type]                    $data      [description]
		 * @param    [type]                    $cacheName [description]
		 * @param    [type]                    $index     [description]
		 */
		public function BuildCache($data,$cacheName,$index){
			$address = array();
			foreach ($data as $k => $v) {
				if(!empty($v['subList'])){
					if($index == 0){
						$this->BuildCache($v['subList'],'AREA_ADDRESS_CITY',1);
					}elseif($index == 1){
						$this->BuildCache($v['subList'],'AREA_ADDRESS_AREA',2);
					}
				}
				unset($v['subList']);
				$address[$v['pid']][$v['id']] = $v;
			}
	
			if(S($cacheName)){
				$address = S($cacheName)+$address;
			}
		
			S($cacheName,$address);
			
		}
	}

	?>